<?php
namespace app\posts\controller;
use app\forum\controller\SiteController;
/**
 * 帖子页面
 */

class ThreadController extends SiteController {

	/**
     * 帖子内容
     */
    public function index()
    {
        $threadId = request('get.thread_id',0,'intval');
        if (empty($threadId)) {
            $this->error404();
        }
        $model = target('ThreadPosts');
        //获取内容信息
        if(!empty($threadId)){
            $threadInfo = $model->getInfo($threadId);
        }else{
            $this->error404();
        }
        //信息判断
        if (!is_array($threadInfo)){
            $this->error404();
        }
        if (!$threadInfo['status']){
            $this->error('该主题已经被屏蔽，您无法查看该主题！');
        }
        //检测权限
        $this->checkForumPurview($threadInfo['plate_id'],'view');
        //获取板块信息
        $modelPlate = target('forum/Plate');
        $plateInfo = $modelPlate->getInfo($threadInfo['plate_id']);
        if (!is_array($plateInfo)){
            $this->error404();
        }
        if($plateInfo['app']<>APP_NAME){
            $this->error404();
        };
        //位置导航
        $crumb = target('forum/Plate')->loadCrumb($threadInfo['plate_id']);
        $this->crumb = array_merge($this->crumb,$crumb);
        //更新访问计数
        $where = array();
        $where['thread_id'] = $threadId;
        target('forum/Thread')->where($where)->setInc('views');
        //内容处理
        $threadInfo['content'] = \app\forum\util\Ubb::ubbToHtml($threadInfo['content']);
        //MEDIA信息
        $media = $this->getMedia($threadInfo['title'],$threadInfo['keywords'],$threadInfo['description']);
        //获取回复列表
        $modelReply = target('forum/Reply');
        $where = array();
        $where['A.thread_id'] = $threadId;
        $pageList = $modelReply->page(20)->loadList($where,10);
        $this->pager = $modelReply->pager;
        $count = $modelReply->countList($where);
        //获取编辑信息
        if($threadInfo['edit_user']){
            $editUserInfo = target('forum/User')->getInfo($threadInfo['edit_user']);
        }
        //URL参数
        $pageMaps = array();
        $pageMaps['thread_id'] = $threadId;
        //获取分页
        $page = $this->getPageShow($pageMaps);
        //模板赋值
        $this->assign('threadInfo', $threadInfo);
        $this->assign('plateInfo', $plateInfo);
        $this->assign('pageList', $pageList);
        $this->assign('editUserInfo', $editUserInfo);
        $this->assign('crumb', $crumb);
        $this->assign('count', $count);
        $this->assign('page', $page);
        $this->assign('media', $media);
        $this->assign('token',target('forum/Thread')->setToken('reply_'.$threadId));
        $this->siteDisplay();
    }

}